/*
 * Yabumi, a 2d scrolling shoot-em-up game.
 * Copyright (C) 2003 Gemma Teruaki <terugen@yabumi.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 */

#include <math.h>
#include <SDL.h>
#include "../sprite.h"
#include "../video.h"
#include "../util.h"
#include "radar.h"

extern _list_header hitcircle_list_header;
extern _list_header item_list_header;

task Radar_Draw(_option *optv)
{
  _Player *player;
  player = (_Player *)optv->socket[0];

  SDL_Surface *image;
  image = Sprite_Radar[0];
  Sprite_Add(player->x, player->y, Z_ORDER_D2, image);

  int i;
  int tangle;
  int dist;
  int vx, vy;
  _list_item *list_item = hitcircle_list_header.next->next;
  _Hitcircle *hitcircle;

  while (list_item != hitcircle_list_header.prev) {
    hitcircle = &list_item->data.Hitcircle;
    if (hitcircle->radar_reaction != __visible) {
      list_item = list_item->next;
      continue;
    };
    tangle = atan3(hitcircle->x - player->x, \
		   hitcircle->y - player->y) / RM_PI;
    dist = distance(hitcircle->x, hitcircle->y, \
		    player->x, player->y) * 0.2;
    if (dist <= 5) {
      list_item = list_item->next;
      continue;
    };
    if (dist <= 200) { 
      i = (optv->count / -5) % 3;
      cap_angle(&tangle);
      image = Sprite_Radar_Arrow[i][tangle];
      vx = dist * cos(tangle * RM_PI);
      vy = dist * sin(tangle * RM_PI);
    } else {
      cap_angle(&tangle);
      image = Sprite_Radar_Arrow_Long[0][tangle];
      vx = 200 * cos(tangle * RM_PI);
      vy = 200 * sin(tangle * RM_PI);
    };
    Sprite_Add(player->x + vx, player->y + vy, Z_ORDER_D2, image);
    list_item = list_item->next;
  };
  
  list_item = item_list_header.next->next;
  _Item *item;

  while (list_item != item_list_header.prev) {
    item = &list_item->data.Item;
    tangle = atan3(item->x - player->x, item->y - player->y) / RM_PI;
    dist = distance(item->x, item->y, player->x, player->y) * 0.2;
    cap_angle(&tangle);
    i = (optv->count / -3) % 2;
    image = Sprite_Radar_Item[i];
    vx = dist * cos(tangle * RM_PI);
    vy = dist * sin(tangle * RM_PI);
    Sprite_Add(player->x + vx, player->y + vy, Z_ORDER_D2, image);
    list_item = list_item->next;
  };

  int sight_x, sight_y;
  sight_x = player->x + LOCKON_SIGHT_DISTANSE * cos(player->angle * RM_PI);
  sight_y = player->y + LOCKON_SIGHT_DISTANSE * sin(player->angle * RM_PI);
  image = Sprite_Radar_Sight[0];
  Sprite_Add(sight_x, sight_y, Z_ORDER_D1, image);
}
